package fi.profecons.csswindows.toolkit;

import java.util.Iterator;
import java.util.List;

import fi.profecons.csswindows.ActionListener;
import fi.profecons.csswindows.Container;

public class Menu extends Container {

	public Menu() {
		super();
		addCommand("new Menu('" + getId() + "');");
	}

	public void addItem(MenuItem menuItem) {
		menuItem.setActionListener(getActionListener());
		getAll().add(menuItem);
		addCommand("getElement('" + getId() + "').addItem(getElement('"
				+ menuItem.getId() + "'));");
	}

	public List getMenuItems() {
		return super.getAll();
	}

	public void setMenuItems(List menuItems) {
		addCommand("getElement('" + getId() + "').clear();");
		getAll().clear();

		Iterator i = menuItems.iterator();
		while (i.hasNext()) {
			MenuItem menuItem = (MenuItem) i.next();
			addItem(menuItem);
		}
	}

	public void setActionListener(ActionListener a) {
		super.setActionListener(a);
		Iterator i = getAll().iterator();
		while (i.hasNext()) {
			MenuItem menuItem = (MenuItem) i.next();
			menuItem.setActionListener(a);
		}
	}

}
